
package com.adee.java;

/*
 * 当new子类对象时：
 * 1.父类static变量和static代码段；
 * 2.子类static变量和static代码段；
 * 3.父类成员变量和代码段；
 * 4.父类构造方法；
 * 5.子类成员变量和代码段；
 * 6.子类构造方法。
 * 原文链接：https://blog.csdn.net/smile_YangYue/article/details/78432135
 * */
public class Test001_FatherChildInit {
    public static void main(String[] args) throws ClassNotFoundException {
        Child1 c1 = new Child1();
        // Child1 c2 = new Child1();
        // Class.forName("com.adee.java.Child1"); // 只加载类，初始化静态成员和执行静态代码块
    }
}

class Parent1 {
    static {
        // System.out.println("country: " + country); // error  illegal forward reference
    }
    private static String country = "NULL";
    private String name;
    private int age;
    public Parent1(String name , int age) {
        this.name = name;
        this.age = age;
        System.out.println("new Parent1");
    }
    static {
        System.out.println("static code > Parent1");
        System.out.println("country: " + country); // NULL
        country = "China";
    }
    {
        System.out.println("code > Parent1");
    }
}

class Child1 extends Parent1 {
    {
        // System.out.println("rank:" + rank); // illegal forward reference
        rank = 5;
    }
    public static String province = "NULL";
    public int rank = 10;
    public Child1() {
        super("", -1);
        rank = -1;
        System.out.println("new Child1");
    }
    static {
        System.out.println("static code > Child1");
    }
    {
        System.out.println("code > Child1, rank:" + rank); // 10
    }
}
